// pages/message/message.js
var common = require("../../public.js");
// import pinyin from "wl-pinyin"
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    selected:3,
    nianList: [],
    active: 1,
    id: 10,
    userInfo: {},
    token: '',
    isLoadingBusy: false,
    pageNum: 1,
    null_data: true
  },
  // 跳转消息详情
  click_detail(e) {
    var that = this;
    let {
      item,
      index
    } = e.currentTarget.dataset

    if (item.isRead == 2) {  //等于2未读设置已读状态
      var tmp = {
        // messageId: item.adminMessageId,
        messageId: item.messageId,
        userId: this.data.userInfo.id,
        token: this.data.token
      }
      common.get('/alipush/updateMessageReadStateById', tmp, res => {
        if (res.status === 200) {
          that.setData({
            [`nianList[${index}].isRead`]: 0
          })
        } else {
          common.showRequestErrMsg(res)
        }
      })
    }

    // 系统消息不跳转
    if (this.data.id == 70) return;

    let page_path, id_num = item.parentId;
    switch (item.jumpType) {
      case 11: // 商品订单
        page_path = '/packageA/pages/Goods/goodsOrder/goodsOrderDetail/index?orderId=';
        break;
      case 12: // 门票订单
        page_path = '/pages/ScenicArea/ScenicAreaOrderDetail/index?orderNo=';
        id_num = item.remark;
        break;
      case 13: // 会员卡订单
        page_path = '/pages/ScenicArea/CardOrderDetail/index?orderNo=';
        id_num = item.remark;
        break;
      case 41: // 活动
        page_path = '/packageB/pages/Activity/actDes/index?name=活动&id=';
        break;
      case 42: // 抽奖
        page_path = '/pages/draw/index?drawId=';
        break;
      case 43: // 团购
        page_path = '/pages/crowd/index/index?id=';
        break;

      default:
        break;
    }
    wx.navigateTo({
      url: page_path + id_num
    })
  },
  onClick(event) {
    var that = this
    this.setData({
      active: event.detail.name,
      nianList: [],
    })

    switch (event.detail.name) {
      case 0:
        that.setData({
          id: 70
        })
        break;
      case 1:
        that.setData({
          id: 10
        })
        break;
      case 2:
        that.setData({
          id: 40
        })
        break;
    }
    this.mess_fn();
  },
  /**
   * 生命周期函数--监听页面加载
   */

  mess_fn() {
    var that = this
    var tmp = {
      userId: that.data.userInfo.id,
      messageType: that.data.id,
      token: that.data.token,
      pageNum: that.data.pageNum,
      pageSize: 10
    }
    common.get('/userMessage/getUserMessage', tmp, res => {
      if (res.status === 200) {
          var list = res.data;
       //   var list = [{"messageId":114241,"messageType":40,"messageTypeName":null,"jumpType":42,"target":"DEVICE","targetValue":"29719","title":"抽奖活动开始了","body":"贵诚购物中心，银座商城，商博瑞，梁师傅给您动大礼啦","remark":"","createTime":1682156408000,"parentId":279,"image":"http://feiyangimage.oss-cn-shanghai.aliyuncs.com/message/1682156399782474.jpeg","isRead":2,"goodsName":"","goodsType":"","orderAmout":0.00,"orderCreateTime":null,"paymentTime":null,"paymentType":""},{"messageId":114241,"messageType":40,"messageTypeName":null,"jumpType":42,"target":"DEVICE","targetValue":"29719","title":"抽奖活动开始了","body":"贵诚购物中心，银座商城，商博瑞，梁师傅给您动大礼啦","remark":"","createTime":1682156408000,"parentId":279,"image":"http://feiyangimage.oss-cn-shanghai.aliyuncs.com/message/1682156399782474.jpeg","isRead":2,"goodsName":"","goodsType":"","orderAmout":0.00,"orderCreateTime":null,"paymentTime":null,"paymentType":""},{"messageId":114241,"messageType":40,"messageTypeName":null,"jumpType":42,"target":"DEVICE","targetValue":"29719","title":"抽奖活动开始了","body":"贵诚购物中心，银座商城，商博瑞，梁师傅给您动大礼啦","remark":"","createTime":1682156408000,"parentId":279,"image":"http://feiyangimage.oss-cn-shanghai.aliyuncs.com/message/1682156399782474.jpeg","isRead":2,"goodsName":"","goodsType":"","orderAmout":0.00,"orderCreateTime":null,"paymentTime":null,"paymentType":""},{"messageId":114241,"messageType":40,"messageTypeName":null,"jumpType":42,"target":"DEVICE","targetValue":"29719","title":"抽奖活动开始了","body":"贵诚购物中心，银座商城，商博瑞，梁师傅给您动大礼啦","remark":"","createTime":1682156408000,"parentId":279,"image":"http://feiyangimage.oss-cn-shanghai.aliyuncs.com/message/1682156399782474.jpeg","isRead":2,"goodsName":"","goodsType":"","orderAmout":0.00,"orderCreateTime":null,"paymentTime":null,"paymentType":""},{"messageId":114241,"messageType":40,"messageTypeName":null,"jumpType":42,"target":"DEVICE","targetValue":"29719","title":"抽奖活动开始了","body":"贵诚购物中心，银座商城，商博瑞，梁师傅给您动大礼啦","remark":"","createTime":1682156408000,"parentId":279,"image":"http://feiyangimage.oss-cn-shanghai.aliyuncs.com/message/1682156399782474.jpeg","isRead":2,"goodsName":"","goodsType":"","orderAmout":0.00,"orderCreateTime":null,"paymentTime":null,"paymentType":""}];
        // 如果数据为空
        if (res.data.length == 0) {
          // 修改加载状态
          that.setData({
            isLoadingBusy: true,
            null_data: false
          })
          return
        };
        for (var i = 0; i < list.length; i++) {
            list[i].createTime = that.time(list[i].createTime)
            list[i].orderCreateTime = that.time(list[i].orderCreateTime)
            list[i].paymentTime = that.time(list[i].paymentTime)
        }
        that.setData({
          nianList: that.data.nianList.concat(list),
          pageNum: that.data.pageNum + 1,
          null_data:list<10?true:false,
          isLoadingBusy:list<10?false:true,
          
        },function(){
            // that.setData({
            //     nianList: list<10,
                
            //   })
        })
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  // 封装时间控件方法
  time(time) {
    return common.dateFormat('yyyy-MM-dd HH:mm', time)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        that.setData({
          userInfo: data.user,
          token: data.token
        }, function () {
          that.mess_fn();
          // wx.hideTabBarRedDot({
          //   index: 3,
          // })
        })
      } else {
        that.setData({
          userInfo: null,
          token: '',
        })
      }
    })
   
  },


      
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom: function () {
  //   console.log('页面上拉触底事件的处理函数')
  //   this.mess_fn();
  // },
  loadMore(){
    this.mess_fn();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})